import matplotlib
import matplotlib.pyplot as plt
from IPython.display import display, clear_output
import time

matplotlib.use('Qt5Agg')
plt.ion()
fig, ax = plt.subplots()
ax.set_title("ECG Signal")
plt.xlabel("time: s")
plt.ylabel("voltage")
#############################
# 1：直接绘制完整波形：
# ax.plot(signal)
# ax.legend(["MLII", "V5"])
# plt.show()
#############################
# 2：随时间动态输出波形：
y1 = []
y2 = []
t = []
sr = 360  # 采样率
plt.xticks(range(0, len(signal) // sr + 1))
for n in range(len(signal)):
    y1.append(signal[n, 0])
    y2.append(signal[n, 1])
    t.append(n / sr)
    if (n + 1) % 10 == 0 or (n + 1) == len(signal):
        time.sleep(0.01)
        ax.plot(t, y1, "-", color='deepskyblue', linewidth=1)
        clear_output(wait=True)
        display(fig)
        plt.pause(0.01)
ax.legend(["MLII", "V5"])
plt.show()

